package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.mess.UpOrDownMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleUpOrDownListener {
    @Autowired
    ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void message(String msg){
        log.info("文章服务收到上下架消息  {}", msg);

        //修改ap_article_config表中的isdown      0商家   1是下架
        if(StringUtils.isNotBlank(msg)){
            UpOrDownMess mess = JSON.parseObject(msg, UpOrDownMess.class);

            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, mess.getArticleId()));
            if(apArticleConfig!=null){
//                apArticleConfig.setIsDown(mess.getEnable()==1 ? false : true);//自媒体传1代表上架   ap_article_config应该存0上架
                apArticleConfig.setIsDown(!(mess.getEnable()==1));//自媒体传1代表上架   ap_article_config应该存0上架
                apArticleConfigMapper.updateById(apArticleConfig);
            }
        }
    }
}
